package com.wnhz.work.yz.fragment;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wnhz.work.yz.MainActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.AllOrderActivity;
import com.wnhz.work.yz.activity.AllProjectActivity;
import com.wnhz.work.yz.activity.AllYuShouActivity;
import com.wnhz.work.yz.activity.CollectActivity;
import com.wnhz.work.yz.activity.ExhibitionActivity;
import com.wnhz.work.yz.activity.FormManagerActivity;
import com.wnhz.work.yz.activity.InstallAllActivity;
import com.wnhz.work.yz.activity.JobActivity;
import com.wnhz.work.yz.activity.LoginNewActivity;
import com.wnhz.work.yz.activity.MemberCenterActivity;
import com.wnhz.work.yz.activity.QRcodeActivity;
import com.wnhz.work.yz.activity.ReceptionAddressActivity;
import com.wnhz.work.yz.activity.ReceptionEvaluateActivity;
import com.wnhz.work.yz.activity.SellAfterActivity;
import com.wnhz.work.yz.activity.SettingActivity;
import com.wnhz.work.yz.activity.TreasureActivity;
import com.wnhz.work.yz.activity.UserInfoManagerActivity;
import com.wnhz.work.yz.activity.YZServiceSelectActivity;
import com.wnhz.work.yz.bean.Fragment4Bean;
import com.wnhz.work.yz.constant.Constants;
import com.wnhz.work.yz.utils.BroadCastReceiverUtil;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * ---我的
 */
public class HomeFragment4 extends Fragment implements View.OnClickListener , BroadCastReceiverUtil.OnReceiveBroadcast{
    private View view;
    private MainActivity activity;

    private ImageView iv_head;
    private TextView tv_name;
    private TextView tv_member;
    private TextView tv_authentication;
    private LinearLayout is_c_nor;
    ImageOptions options = new ImageOptions.Builder()
            //设置加载过程中的图片
            .setLoadingDrawableId(R.mipmap.ic_morentouxiang)
            //设置加载失败后的图片
            .setFailureDrawableId(R.mipmap.ic_morentouxiang)
            //设置使用缓存
            .setUseMemCache(true)
            //设置显示圆形图片
            .setCircular(true)
            //设置支持gif
            .setIgnoreGif(false)
            .build();


    BroadcastReceiver broadcastReceiver;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home_fragment4, container, false);
        activity = (MainActivity) getActivity();
        initData();
        initView();
        broadcastReceiver = BroadCastReceiverUtil.registerBroadcastReceiver(getContext(), new String[]{ Constants.LOGO_SUCCESS}, HomeFragment4.this);
        return view;
    }

    private void initData() {

    }

    private void initView() {
        view.findViewById(R.id.rl_news).setOnClickListener(this);
        view.findViewById(R.id.rl_setting).setOnClickListener(this);
        view.findViewById(R.id.rl_personal).setOnClickListener(this);
        view.findViewById(R.id.rl_all).setOnClickListener(this);
        view.findViewById(R.id.rl_pay).setOnClickListener(this);
        view.findViewById(R.id.rl_send).setOnClickListener(this);
        view.findViewById(R.id.rl_true).setOnClickListener(this);
        view.findViewById(R.id.rl_end).setOnClickListener(this);
        view.findViewById(R.id.rl_items).setOnClickListener(this);
        view.findViewById(R.id.rl_treasure).setOnClickListener(this);
        view.findViewById(R.id.rl_member).setOnClickListener(this);
        view.findViewById(R.id.rl_code).setOnClickListener(this);
        view.findViewById(R.id.rl_collect).setOnClickListener(this);
        view.findViewById(R.id.rl_address).setOnClickListener(this);
        view.findViewById(R.id.rl_service).setOnClickListener(this);
        view.findViewById(R.id.rl_exhibition).setOnClickListener(this);
        view.findViewById(R.id.rl_work).setOnClickListener(this);
        view.findViewById(R.id.rl_yushou).setOnClickListener(this);
        view.findViewById(R.id.rl_form).setOnClickListener(this);
        view.findViewById(R.id.rl_install).setOnClickListener(this);
        is_c_nor = (LinearLayout) view.findViewById(R.id.is_c_nor);
        if (MyApplication.getInstance().getUserBean() != null && MyApplication.getInstance().getUserBean().getIs_c().equals("1") || MyApplication.getInstance().getUserBean() == null) {
            is_c_nor.setVisibility(View.GONE);
        } else {
            is_c_nor.setVisibility(View.VISIBLE);
        }
        iv_head = (ImageView) view.findViewById(R.id.iv_head);
        tv_name = (TextView) view.findViewById(R.id.tv_name);
        tv_member = (TextView) view.findViewById(R.id.tv_member);
        tv_authentication = (TextView) view.findViewById(R.id.tv_authentication);
//        upload();
    }

    private static final String TAG = "HomeFragment4";

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            //消息
            case R.id.rl_news:
                intent = new Intent(getActivity(), ReceptionEvaluateActivity.class);
                startActivity(intent);
                break;

            //设置
            case R.id.rl_setting:
                intent = new Intent(activity, SettingActivity.class);
                startActivity(intent);
                break;

            //账户管理
            case R.id.rl_personal:
                intent = new Intent(activity, UserInfoManagerActivity.class);
                startActivity(intent);
                break;

            //全部订单
            case R.id.rl_all:
                intent = new Intent(activity, AllOrderActivity.class);
                intent.putExtra("Page", "0");
                intent.putExtra("type", "0");
                startActivity(intent);
                break;

            //待付款
            case R.id.rl_pay:
                intent = new Intent(activity, AllOrderActivity.class);
                intent.putExtra("Page", "1");
                startActivity(intent);
                break;

            //待发货
            case R.id.rl_send:
                intent = new Intent(activity, AllOrderActivity.class);
                intent.putExtra("Page", "2");
                startActivity(intent);
                break;

            //待确认
            case R.id.rl_true:
                intent = new Intent(activity, AllOrderActivity.class);
                intent.putExtra("Page", "3");
                startActivity(intent);
                break;

            //售后
            case R.id.rl_end:
                //  activity.MyToast("售后");
                startActivity(new Intent(getContext(), SellAfterActivity.class));
                break;

            //项目  (我的 项目)
            case R.id.rl_items:
                intent = new Intent(getActivity(), AllProjectActivity.class);
                intent.putExtra("type", "0");
                startActivity(intent);
                break;
            //预售列表
            case R.id.rl_yushou:
                intent = new Intent(getActivity(), AllYuShouActivity.class);
                startActivity(intent);
                break;

            //工作
            case R.id.rl_work:
                startActivity(new Intent(getContext(), JobActivity.class));
                break;
            //安装订单
            case R.id.rl_install:
                intent = new Intent(getActivity(), InstallAllActivity.class);
                startActivity(intent);
                break;   //安装订单

      /*      //备案
            case R.id.rl_records:
                activity.MyToast("备案");
                startActivity(new Intent(getContext(), PerfectInformationActivity.class));
                startActivity(new Intent(getContext(), AddCartActivity.class));
                break;*/

            //财富宝
            case R.id.rl_treasure:
                intent = new Intent(getActivity(), TreasureActivity.class);
                startActivity(intent);
                break;


            //会员中心
            case R.id.rl_member:
                intent = new Intent(getActivity(), MemberCenterActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("class_name", class_name);
                intent.putExtra("personImg", personImg);
                startActivity(intent);
                break;

            //邀请二维码
            case R.id.rl_code:
                intent = new Intent(getActivity(), QRcodeActivity.class);
                startActivity(intent);
                break;

            //收藏
            case R.id.rl_collect:
                intent = new Intent(getActivity(), CollectActivity.class);
                startActivity(intent);
                break;

            //收货地址
            case R.id.rl_address:
                intent = new Intent(getActivity(), ReceptionAddressActivity.class);
                startActivity(intent);
                break;
            //发票管理
            case R.id.rl_form:
                intent = new Intent(getActivity(), FormManagerActivity.class);
                startActivity(intent);
                break;
            //客服中心
            case R.id.rl_service:
                if (MyApplication.getInstance().getUserBean() == null) {
                    Toast.makeText(getContext(), "请登录", Toast.LENGTH_SHORT).show();
                    Intent intent2 = new Intent(getContext(), LoginNewActivity.class);
                    startActivity(intent2);
                    return;
                }
                intent = new Intent(getActivity(), YZServiceSelectActivity.class);
                startActivity(intent);
                break;

            //展馆
            case R.id.rl_exhibition:
                intent = new Intent(getActivity(), ExhibitionActivity.class);
                startActivity(intent);
                break;

            default:
                break;
        }
    }

    public void uphead() {
        if (MyApplication.getInstance().getUserBean() != null) {
            x.image().bind(iv_head, MyApplication.getInstance().getUserBean().getHeadUrl(), options);
            //Log.i(TAG, "initView: " + MyApplication.getInstance().getUserBean().getHeadUrl());
            upload();
        }
    }

    private String name, class_name, personImg;
    private ArrayList<Fragment4Bean.AreaEntity> areaList = new ArrayList<>();
    private String isHuaWuYuan = "0";

    public void upload() {
        if (MyApplication.getInstance().getUserBean() == null) {
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        Log.e("===to", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.MEMBER_GETUSERINFO, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    if (res.equals("-1")) {
                        tv_member.setText("未登录");
                        tv_name.setText("未登录");
                        tv_authentication.setText("未审核");
                        // activity.MyToast("请重新登录");
                        //info = "请重新登录";
//                        activity.MyToast("请重新登录");
//                        startActivity(new Intent(activity, LoginNewActivity.class));
//                        return;
                    } else {
                        Log.i(TAG, "onSuccess: My=== " + result);
                        Fragment4Bean bean = new Fragment4Bean();
                        Gson gson = new Gson();
                        bean = gson.fromJson(result, Fragment4Bean.class);
                        tv_member.setText(bean.getClassname() + "");
                        MyApplication.getInstance().getUserBean().setUserName(bean.getRealname());
                        MyApplication.getInstance().getUserBean().setClassName(bean.getClassname());
                        MyApplication.getInstance().getUserBean().setHeadLogo(bean.getPerson_img());
                        tv_name.setText(bean.getRealname());
                        //0资料未审核 1资料通过审核 2审核失败 3提交审核
                        if (bean.getIsexamine().equals("0")) {
                            tv_authentication.setText("未审核");
                        } else if (bean.getIsexamine().equals("1")) {
                            tv_authentication.setText("已审核");
                        } else if (bean.getIsexamine().equals("2")) {
                            tv_authentication.setText("审核失败");
                        } else if (bean.getIsexamine().equals("3")) {
                            tv_authentication.setText("待审核");
                        }
                        name = bean.getRealname();
                        personImg = bean.getPerson_img();
                        class_name = bean.getClassname();
                        areaList = (ArrayList<Fragment4Bean.AreaEntity>) bean.getArea();
                        isHuaWuYuan = bean.getIs_huawuyuan();
                        MyApplication.getInstance().getUserBean().setAreaList(areaList);
                        MyApplication.getInstance().getUserBean().setIsHuaWuYuan(isHuaWuYuan);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onResume() {
        uphead();
        if (MyApplication.getInstance().getUserBean() != null && MyApplication.getInstance().getUserBean().getIs_c().equals("1") || MyApplication.getInstance().getUserBean() == null) {
            is_c_nor.setVisibility(View.GONE);
        } else {
            is_c_nor.setVisibility(View.VISIBLE);
        }
        super.onResume();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (is_c_nor != null)
            if (MyApplication.getInstance().getUserBean() != null && MyApplication.getInstance().getUserBean().getIs_c().equals("1") || MyApplication.getInstance().getUserBean() == null) {
                is_c_nor.setVisibility(View.GONE);
            } else {
                is_c_nor.setVisibility(View.VISIBLE);
            }
        super.onHiddenChanged(hidden);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Constants.LOGO_SUCCESS)) {
            uphead();
        }
    }
}
